﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication.Models;
using System.Web.Security;

namespace MvcApplication.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(AccountModel account)
        {
            DataAccessLayer.AdoDbContext context = new DataAccessLayer.AdoDbContext();
            if (ModelState.IsValid)
            if(context.IsUserValid(account.Login,account.Password))
            {
                
               FormsAuthentication.SetAuthCookie(account.Login, account.IsRememberMe);
               return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect! Type this again");
            }
            return View();
        }

        public ActionResult Logout()
        {
            if (User.Identity.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
                Response.Redirect("~/Account/Logout");

            }
            return RedirectToAction("Login", "Account");
        }
    }
}
